package com.crm.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.crm.dao.PhotosDao;
import com.crm.entity.Photos;
import com.crm.service.PhotosService;

@Service
public class PhotosServiceImpl implements PhotosService {

	@Autowired
	private PhotosDao photosDao;

	@Override
	public List<Photos> findPhotosByCategory(String category) {
		if(StringUtils.isNotBlank(category) && "0".equals(category)) {
			return photosDao.findAll();
		}
		return photosDao.findByCategory(category);
	}

	@Override
	public void addPhotos(Photos photos) {
		photos.setAddTime(new Date());
		if(photos.getClick() == null) {
			photos.setClick(0);
		}
		photosDao.save(photos);
		
	}

	@Override
	public Photos getPhotosById(String id) {
		if(StringUtils.isNotBlank(id)) {
			return photosDao.findOne(id);
		}
		return null;
	}

	@Override
	public Page<Photos> getPhotosByPage(Integer pageIndex, String title) {
		Photos photos = new Photos();
		if(StringUtils.isNotBlank(title)) {
			photos.setTitle(title);
		}
		Example<Photos> example = Example.of(photos);
		PageRequest pageable = new PageRequest(pageIndex, 10,Sort.Direction.DESC,"addTime");
		
		return photosDao.findAll(example, pageable);
	}
	
}
